package com.vercon.moensmart.newtork.mode;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignMode implements Serializable {

    public String appid;
    public String appSerect;
    public String nonce;
    public String ts;
    private String sign;

    public SignMode(String appid,String appSerect,String nonce,String ts ){
        this.appid = appid;
        this.appSerect = appSerect;
        this.nonce = nonce;
        this.ts = ts;
    }

    public String getSign(){
        String afterStr = this.appid + "," + this.appSerect + "," + this.nonce + "," + this.ts;
        sign = encryptMD5(afterStr).toUpperCase();
        return  sign;
    }
    public String encryptMD5(String input) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(input.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuilder hexString = new StringBuilder();
            for (byte aMessageDigest : messageDigest) {
                String h = Integer.toHexString(0xFF & aMessageDigest);
                while (h.length() < 2) h = "0" + h;
                hexString.append(h);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public String toString() {
        return "SignMode{" +
                "appid='" + appid + '\'' +
                ", appSerect='" + appSerect + '\'' +
                ", nonce='" + nonce + '\'' +
                ", ts='" + ts + '\'' +
                ", sign='" + sign + '\'' +
                '}';
    }
}
